package com.lightkits.wms.domain.mainpage.service.impl;

import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.wms.database.dao.WmsWarehouseMapper;
import com.lightkits.wms.database.model.WorkerInfoModel;
import com.lightkits.wms.dto.mainpage.model.ResourceInfoDTO;
import com.lightkits.wms.domain.mainpage.repo.FndWorkerGroupDBService;
import com.lightkits.wms.domain.mainpage.service.WorkerInfoDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * WorkerInfoDomainServiceImpl
 *
 * @author menxiao.wu@hand-china.com
 * Created on 2019-02-20 20:44
 */
@Service
public class WorkerInfoDomainServiceImpl implements WorkerInfoDomainService {
    private final FndWorkerGroupDBService service;

    @Autowired
    public WorkerInfoDomainServiceImpl(FndWorkerGroupDBService service) {
        this.service = service;
    }
    @Autowired
    private WmsWarehouseMapper wmsWarehouseMapper;

    @Override
    public List<ResourceInfoDTO> listWorkerInfo(String organizationId,
                                                String workerGroupCode,
                                                String workerNumOrName) {
        List<ResourceInfoDTO> resourceInfoDTOS = new ArrayList<>();
//
//        List<FndWorkerGroup> fndworkerGroups = new FndWorkerGroup().selectList(new EntityWrapper()
//                .eq(FndWorkerGroup.ORGANIZATION_ID, organizationId)
//                .eq(FndWorkerGroup.WORKER_GROUP_CODE, workerGroupCode));
//        if (!ObjectUtil.isEmpty(fndworkerGroups)) {
//            List<Long> baseIds = fndworkerGroups.stream().map(FndWorkerGroup::getId).collect(Collectors.toList());
//
//            List<FndWorker> fndWorkers = new FndWorker().selectList(new EntityWrapper<FndWorker>()
//                    .in(FndWorker.WORKER_GROUP_ID, baseIds)
//                    .like(FndWorker.WORKER_NUMBER,workerNumAndName));
//            if (!ObjectUtil.isEmpty(fndWorkers)) {
//                List<Long> workerIds = fndWorkers.stream().map(FndWorker::getId).collect(Collectors.toList());
//                List<FndResourceTl> fndResourceTls = new FndResourceTl().selectList(new EntityWrapper<FndResourceTl>()
//                        .in(FndResourceTl.RESOURCE_ID, workerIds)
//                        .like(FndResourceTl.RESOURCE_NAME,workerName));
//                if (!ObjectUtil.isEmpty(fndResourceTls)) {
//                    resourceInfoDTOS = fndResourceTls.stream().map(
//                            t -> {
//                                ResourceInfoDTO dto = new ResourceInfoDTO();
//                                dto.setWorkerId(ObjectUtil.toString(t.getResourceId()));
//                                dto.setWorkerName(t.getResourceName());
//                                Optional<FndWorker> opt = fndWorkers.stream().filter(target -> target.getId().equals(t.getResourceId())).findFirst();
//                                if (opt.isPresent()) {
//                                    dto.setWorkerCode(opt.get().getWorkerNumber());
//                                }
//                                return dto;
//                            }
//                    ).collect(Collectors.toList());
//                }
//            }
//        }
        List<WorkerInfoModel> models=wmsWarehouseMapper.getWorekrInfoByParams(organizationId,workerGroupCode,workerNumOrName);
        if(ObjectUtil.isNullOrEmpty(models)){
            return resourceInfoDTOS;
        }
        models.forEach(target->{
            ResourceInfoDTO dto=new ResourceInfoDTO();
            dto.setWorkerId(target.getWorkerId());
            dto.setWorkerCode(target.getWorkerCode());
            dto.setWorkerName(target.getWorkerName());
            resourceInfoDTOS.add(dto);
        });
        return resourceInfoDTOS;
    }
}
